/*
 * @(#)TestsetAction.java
 *
 *
 *
 * ver 1.0 Sep 6, 2007 ggranum
 */
package org.testfire.realm.view.json.action;

import org.testfire.realm.control.agent.AgentManager;
import org.testfire.realm.control.auth.user.UserDBAO;
import org.testfire.realm.control.exception.UpdateException;
import org.testfire.realm.control.testfire.TestFireDBAO;
import org.testfire.realm.control.testfire.queue.QueueBean;
import org.testfire.realm.control.user.UserBean;
import org.testfire.realm.model.entities.testfire.TestRunnerQueueEntity;
import org.testfire.realm.view.json.translator.AgentTranslator;
import org.testfire.realm.view.json.translator.QueueTranslator;
import org.testfire.realm.view.json.translator.TestsetTranslator;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

// eof: TestsetAction.java

public class RunQueueNowAction implements JsonAction<String> {

    TestFireDBAO testfireDBAO;
    QueueBean queueBean;
    TestsetTranslator testsetTranslator;
    AgentTranslator agentTranslator;
    QueueTranslator queueTranslator;

    UserBean userBean;

    // Inject the testfire entity manager, as defined in the persistence.xml file.
    @PersistenceContext(unitName = "testfire")
    EntityManager em;

    UserDBAO userDBAO;


    public String perform( String[] argumentKeys, String... args ) throws UpdateException {


        // 'add' is adding an existing row:
        Integer id = Integer.valueOf(args[0]);
        TestRunnerQueueEntity queue = em.find(TestRunnerQueueEntity.class, id);
        AgentManager agentMan = (AgentManager) testfireDBAO.getValueExpresionValue("AgentManager");
        agentMan.enqueueTestset(queue);
        this.testfireDBAO.merge(queue);

        return "success";

    }


    public void setTestfireDBAO(TestFireDBAO testfireDBAO) {
        this.testfireDBAO = testfireDBAO;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }


    public void setQueueBean(QueueBean queueBean) {
        this.queueBean = queueBean;
    }

    public void setTestsetTranslator(TestsetTranslator testsetTranslator) {
        this.testsetTranslator = testsetTranslator;
    }

    public void setQueueTranslator(QueueTranslator queueTranslator) {
        this.queueTranslator = queueTranslator;
    }

    public void setAgentTranslator(AgentTranslator agentTranslator) {
        this.agentTranslator = agentTranslator;
    }
}